package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Sheliandt;
import util.Pagination;
import db.DBConnect;

public class SheliandtControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author dujuan
	 * @return
	 * 2012-02-29
	 */
	public boolean insertSheliandt(Sheliandt sheliandt)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into sheliandt (dongxiaoxiName,dongxiaoxiTime,dongxiaoxiNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, sheliandt.getDongxiaoxiName());
			dbc.setString(2, sheliandt.getDongxiaoxiTime());
			dbc.setString(3, sheliandt.getDongxiaoxiNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	
	/**
	 * 根据id获取信息
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public Sheliandt GetSheliandt(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from sheliandt where dongxiaoxiId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Sheliandt sheliandt = new Sheliandt();
			
			if(rs.next())
			{
				sheliandt.setDongxiaoxiId(rs.getInt("dongxiaoxiId"));
				sheliandt.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
				sheliandt.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
				sheliandt.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
				
				return 	sheliandt;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteSheliandt(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from sheliandt where dongxiaoxiId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-29
	 */
	
	public boolean updateSheliandt (Sheliandt sheliandt, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update sheliandt set dongxiaoxiName=? ,dongxiaoxiTime=?,dongxiaoxiNeirong=? where dongxiaoxiId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, sheliandt.getDongxiaoxiName());
			dbc.setString(2, sheliandt.getDongxiaoxiTime());
			dbc.setString(3, sheliandt.getDongxiaoxiNeirong());
			dbc.setInt(4, id);
			
			//System.out.println("~~~: " + id);
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetSheliandt()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from sheliandt order by dongxiaoxiId desc";
		dbc.prepareStatement(sql);
		Collection<Sheliandt> sheliandtList = new ArrayList<Sheliandt>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Sheliandt sheliandt = new Sheliandt();
				sheliandt.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
				sheliandt.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
				sheliandt.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
			}
			return sheliandtList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetSheliandt(int page, int rowsPerPage)
	{
		String sql = "select * from sheliandt order by dongxiaoxiId desc";
		pageInstance = new SheliandtControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class SheliandtControlPage extends Pagination{
	public SheliandtControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Sheliandt> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Sheliandt> sheliandtList = new ArrayList<Sheliandt>();
		while(rs.next())
		{
			Sheliandt sheliandt = new Sheliandt();
			
			sheliandt.setDongxiaoxiId(rs.getInt("dongxiaoxiId"));	
			sheliandt.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
			sheliandt.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
			sheliandt.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
			
				
			sheliandtList.add(sheliandt);
			sheliandt = null;
			}
			return sheliandtList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
